<template>
	<view>
		<nav-bar :home='false' ref="navBar" :scrollTop="scrollTop" transparentFixedFontColor="#FFF" type="transparentFixed"
		 title="">
			<view class="transparent_fixed_preview" slot="transparentFixedRight" @click="onPreview"></view>
		</nav-bar>

		<view style="height: 375rpx;position: relative;display: flex;justify-content: center;" class="registerImg">
			<view class="regiestTitle">注册</view>
		</view>
		<view class="login_body">
			<u-input class="iptas" :border="true" v-model="phone" :type="type" placeholder="请输入手机号" />
			<view>
				<view class="codeinput">
					<u-input class="iptas" :border="true" v-model="captcha" :type="type" placeholder="请输入验证码" />
					<view class="wrap">
						<u-toast ref="uToast"></u-toast>
						<u-verification-code :seconds="seconds" @end="end" @start="start" ref="uCode" @change="codeChange"></u-verification-code>
						<text @tap="getCode" style="color: #05B2B5;">{{ tips }}</text>
					</view>
				</view>
			</view>
			<u-input class="iptas" :border="true" v-model="password" type="password" placeholder="请输入密码" />
			<view class="btnLogin" @click="toRegister">注册</view>
			<view class="agreement">
				<u-radio-group v-model="value">
					<u-radio shape="circle">我已阅读并同意《用户协议》与《隐私政策》</u-radio>
				</u-radio-group>
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				value: '',
				type: 'text',
				phone: '',
				password: '',
				captcha: '',
				scrollTop: 0,
				tips: '',
				// refCode: null,
				seconds: 60
			};
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if(!this.phone){
					this.$refs.uToast.show({
						title: '请输入手机号',
						type: 'error'
					});
					return;
				}
				let data = {
					phone: this.phone
				};
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					});
					this.$u.api.getcaptcha(data).then(res => {
						uni.hideLoading();
						this.$u.toast('验证码已发送');
						this.$refs.uCode.start();
					});
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			end() {
				this.$u.toast('倒计时结束');
			},
			start() {
				this.$u.toast('倒计时开始');
			},
			btnRegister() {
				uni.navigateTo({
					url: './register'
				});
			},
			// 注册
			toRegister() {
				if(!this.phone){
					this.$refs.uToast.show({
						title: '请填写手机号',
						type: 'error'
					});
				}
				this.$u.api.getregister({
					password: this.password,
					phone: this.phone,
					validation: this.captcha
				}).then(res => {
					this.$refs.uToast.show({
						title: '注册成功',
						type: 'success'
					});
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.login_body {
		position: absolute;
		background: #fff;
		top: 350rpx;
		width: 100%;
		margin: 0 auto;
		border-radius: 40rpx;
		padding-top: 50rpx;
	}

	.title_top {
		display: flex;
		border: 1rpx solid #eeeeee;
		font-size: 35rpx;
		margin-top: 20rpx;

		>view {
			padding: 20rpx 40rpx;
			flex: 1;
			text-align: center;
		}
	}

	.islogin {
		background: #7167fc;
		color: #fff;
	}

	.iptas {
		padding: 30rpx 20rpx;
		margin: 0 auto;
		width: 70%;
		border-bottom: 1rpx solid #eee;
		margin-top: 40rpx;
	}

	.btnLogin {
		width: 70%;
		background: #05B2B5;
		text-align: center;
		padding: 20rpx;
		color: #FFFFFF;
		border-radius: 50rpx;
		font-size: 32rpx;
		position: fixed;
		bottom: 110rpx;
		left: 50%;
		transform: translate(-50%, -100%);
	}

	.codeinput {
		position: relative;
	}

	.wrap {
		text-align: right;
		font-size: 24rpx;
		margin-right: 90rpx;
		margin-top: 20rpx;
	}

	.registerImg {
		background-image: url(../../static/img/login_top.png);
		background-size: 100% 100%;
	}

	.regiestTitle {
		position: fixed;
		top: 150rpx;
		left: 60rpx;
		font-size: 48rpx;
		color: #fff;
	}

	.agreement {
		font-size: 25rpx;
		color: #b6b6b6;
		text-align: center;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 120rpx;
	}
</style>
